/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.initilize;

import com.wtkj.exception.BusinessException;
import com.wtkj.vo.RandomGUID;
import com.wtkj.vo.dzzf.base.kfwd.Account;
import com.wtkj.vo.dzzf.base.kfwd.AccountOpt;
import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.share.staticvalue.BusinessType;
import com.wttech.dzzf.process.dao.DataInitilizingDao;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.model.user.User;
import com.wttech.dzzf.process.model.user.UserInfoOpt;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 业务账户处理服务实现
 * Created by MaShibo on 2017/10/23.
 */
@Service
public class AccountProcessService {

	private static final Logger LOGGER = LoggerFactory.getLogger(AccountProcessService.class);

	private static final String TRADENO_STR = "TRADENO";

	@Resource
	private DataInitilizingDao dataInitilizingDao;

	@Resource
	private TransferUserInfoDao transferUserInfoDao;

	@Resource
	private CardInfoProcessService cardInfoProcessService;

	public  void processAccount(Account account, User user) {
		Validate.notNull(account, "业务账户信息不能为空");
		String accountId = account.getAccountId();
		createUserInfoOpt(account, user);

		List<CardInfo> cardInfos = dataInitilizingDao.queryCardInfosByAccountId(accountId);
		cardInfos.forEach(cardInfo -> cardInfoProcessService.processCardInfo(cardInfo, user));
	}

	/**
	 * 创建客户信息开户记录
	 *
	 * @param account 业务账户信息
	 * @param user    客户信息
	 */
	private void createUserInfoOpt(Account account, User user) {
		String tradeNo = null;
		Object tradeNoObj = account.getExtendProperties().get(TRADENO_STR);
		if (tradeNoObj != null) {
			tradeNo = tradeNoObj.toString();
		}
		if (StringUtils.isNotBlank(tradeNo)) {
			UserInfoOpt userInfoOpt = new UserInfoOpt();
			userInfoOpt.setOptId(new RandomGUID().ToRawString());
			userInfoOpt.setTradeNo(tradeNo);
			userInfoOpt.setUserId(user.getId());
			userInfoOpt.setUserType(user.getUserType());
			//默认确认状态
			userInfoOpt.setState(1);
			try {
				transferUserInfoDao.createTestObjcet(userInfoOpt);
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
				LOGGER.info("错误的客户操作记录{}", userInfoOpt);
				throw new BusinessException("客户信息开户记录创建错误！");
			}
		} /*else {
			createAccountOpt(account);
			createUserInfoOpt(account, user);
		}*/
	}


	private AccountOpt createAccountOpt(Account account) {
		AccountOpt accountOpt = new AccountOpt();
		accountOpt.setOptId(new RandomGUID().toString());
		accountOpt.setAccountBalance(BigDecimal.ZERO);
		accountOpt.setAccountId(account.getAccountId());
		accountOpt.setAccountNo(account.getAccountNo());
		accountOpt.setClientNO(account.getClientNO());
		accountOpt.setFundAccountId(account.getFundAccountId());
		accountOpt.setOptOn(account.getCreateOn());
		accountOpt.setOptType(BusinessType.KHSQ);
		accountOpt.setTradeNo(new RandomGUID().ToRawString());
		//已确认
		accountOpt.setState(1);
		accountOpt.setTopClass(account.getTopClass());
		try {
			transferUserInfoDao.createTestObjcet(accountOpt);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			throw new BusinessException("创建开户操作记录失败！");
		}
		account.getExtendProperties().put(TRADENO_STR, accountOpt.getTradeNo());
		return accountOpt;
	}

}
